<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var maxPathSum = function (root) {
        let maxSum = root.val
        postOrder(root)
        return maxSum
        function postOrder(root) {
          if (!root) return 0
          let leftSum = postOrder(root.left)
          let rightSum = postOrder(root.right)
          maxSum = Math.max(maxSum, root.val + leftSum + rightSum)
          //左右子树只能取一个或者都不取
          let returnSum = Math.max(0, leftSum, rightSum) + root.val
          return returnSum > 0 ? returnSum : 0
        }
      }
    </script>
  </body>
</html>
